#import <Foundation/Foundation.h>


//============== START CLASS: TIRE ==================
@interface Tire : NSObject
{
    //No instance variables....
}
@end


@implementation Tire

- (NSString *) Description
{
    return (@"I am a tire. I last a while");
}

@end
//============== END CLASS: TIRE ==================





//============== START CLASS: ENGINE ==================
@interface Engine : NSObject
{
    //No instance variables....
}

@end


@implementation Engine

- (NSString *) Description
{
    return (@"I am an Engine. Vrooom!");
}

@end
//============== END CLASS: ENGINE ==================





//============== START CLASS: CAR ==================
@interface Car : NSObject
{
    //Instance variables....
    Engine *engine; //D001: Engine ptr
    Tire *tires[4]; //D002: C-Style array
}

- (Engine *) engine;                        //M001: get method for Engine
- (void) setEngine: (Engine *) newEngine;   //M002: set method for Engine

- (Tire *) tireAtIndex: (int) index;        //M003: get method for Tire @ someIndex
- (void) setTire: (Tire *) tire 
         atIndex: (int) index;              //M004: set method for Tire @ someIndex

-(void) print;                              //M005: print output

@end


@implementation Car

//M001:
- (Engine *) engine 
{
    return (engine); 
}

//M002:
- (void) setEngine: (Engine *)newEngine
{
    engine = newEngine;
}

//M003:
- (Tire *) tireAtIndex:(int)index
{
    //check index bounds...
    if (index < 0 || index > 3) 
    {
        NSLog( @"\nBad index: %d, in tireAtIndex\n", index ); 
        exit(1);
    }
    
    return ( tires[index] );
}

//M004:
- (void) setTire:(Tire *)tire atIndex:(int)index
{
    //check index bounds...
    if (index < 0 || index > 3) 
    {
        NSLog( @"\nBad index: %d, in tireAtIndex\n", index ); 
        exit(1);
    }
    
    tires[index] = tire;
}

//M005:
- (void) print
{
    NSLog( @ "%@", [engine Description] );
    
    for (int i=0; i< 4; i++) 
    { 
        NSLog( @"%@", [tires[i] Description] ); 
    }//for-loop
}
@end

//============== END CLASS: CAR ==================





int main (int argc, const char * argv[]) 
{
    Car *car = [Car new];
    Engine *engine = [Engine new];
    
    [ car setEngine: engine ];
    
    
    for (int i=0; i< 4; i++) 
    {
        Tire * tire = [Tire new];
        
        [car setTire:tire atIndex:i];
    }
    
    [car print];
    
    return 0;
}
